package com.atguigu.cloud.handler;

import com.atguigu.crowd.api.MySQLRemoteService;
import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.vo.AddressVO;
import com.atguigu.crowd.entity.vo.MemberLoginVO;
import com.atguigu.crowd.entity.vo.OrderProjectVO;
import com.atguigu.crowd.util.ResultEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * information
 * Created by Administrator on 2020/4/6.
 */
@Controller
public class OrderHandler {

    @Autowired
    private MySQLRemoteService mySQLRemoteService;

    private Logger logger = LoggerFactory.getLogger(OrderHandler.class);

    @RequestMapping("/save/address")
    public String saveAddressVO(AddressVO addressVO,HttpSession session){
        //1.执行地址信息的保存
       ResultEntity<String> resultEntity = mySQLRemoteService.saveAddressVORemote(addressVO);
        logger.info("地址保存处理结果："+resultEntity.getResult());
       //2.从session域中获取orderProjectVO对象
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        //3.从对象中获取returnCount
        Integer returnCount = orderProjectVO.getReturnCount();
        //4.重定向到指定的地址
        return "redirect:http://localhost/order/confirm/order/"+returnCount;
    }


    @RequestMapping("/confirm/order/{returnCount}")
    public String showConfirmOrderInfo(@PathVariable("returnCount") String returnCount,
                                       HttpSession session){
        //1.把接收到的回报数量合并到Session域中
        OrderProjectVO orderProjectVO = (OrderProjectVO) session.getAttribute("orderProjectVO");
        orderProjectVO.setReturnContent(returnCount);
        session.setAttribute("orderProjectVO",orderProjectVO);

        //2.获取当前已登录的用户ID
        MemberLoginVO memberLoginVO = (MemberLoginVO)session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_MEMBER);
        Integer memberId = memberLoginVO.getId();
        //3.查询目前收货地址
        ResultEntity<List<AddressVO>> resultEntity = mySQLRemoteService.getAddressVORemote(memberId);
        String result = resultEntity.getResult();
        if (ResultEntity.SUCCESS.equals(result)){
            List<AddressVO> addressVOList = resultEntity.getData();
            session.setAttribute("addressVOList",addressVOList);
        }

        return "confirm-order";
    }




    @RequestMapping("/confirm/return/info/{projectId}/{returnId}")
    public String showReturnConfirmInfo(
            @PathVariable("projectId") Integer projectId,
            @PathVariable("returnId") Integer returnId,
            HttpSession session) {

        ResultEntity<OrderProjectVO> resultEntity =
                mySQLRemoteService.getOrderProjectVORemote(projectId, returnId);

        if(ResultEntity.SUCCESS.equals(resultEntity.getResult())) {
            OrderProjectVO orderProjectVO = resultEntity.getData();

            // 为了能够在后续操作中保持orderProjectVO数据，存入Session域
            session.setAttribute("orderProjectVO", orderProjectVO);
        }

        return "confirm-return";
    }




}
